// 将接口AppTree数据转成Select Options格式
var ToSelectOptions = function(data) {
  return GetSelectOptionsData(data, 1);
};
var ToTreeData = function(data) {
  return GetTreeData(data, 1);
};

function GetSelectOptionsData(data, level) {
  var resObject = [];
  if (!data || data.length < 1) {
    return null;
  }
  data.forEach((v, i) => {
    var thisId = v.id.toString();
    if (v.id === -1) {
      thisId = "Tree" + level + "" + i;
    }
    if (v.children && v.children.length > 0) {
      resObject.push({
        value: thisId,
        label: v.label,
        children: GetSelectOptionsData(v.children, level + 1)
      });
    } else {
      resObject.push({
        value: thisId,
        label: v.label,
        children: GetSelectOptionsData(v.children, level + 1)
      });
    }
  });
  return resObject;
}
function GetTreeData(data, level) {
  var resObject = [];
  if (!data || data.length < 1) {
    return null;
  }
  data.forEach((v, i) => {
    var thisId = v.id.toString();
    if (v.id === -1) {
      thisId = "Tree" + level + "" + i;
    }
    if (v.children && v.children.length > 0) {
      resObject.push({
        id: thisId,
        label: v.label,
        children: GetTreeData(v.children, level + 1)
      });
    } else {
      resObject.push({
        id: thisId,
        label: v.label,
        children: GetTreeData(v.children, level + 1)
      });
    }
  });
  return resObject;
}
// 增加disabled
function GetTreeDataDisabled(data, level) {
  var resObject = [];
  if (!data || data.length < 1) {
    return null;
  }
  data.forEach((v, i) => {
    var thisId = v.id.toString();
    var dis = false;
    if (v.id === -1) {
      thisId = "Tree" + level + "" + i;
      dis = true;
    }
    if (v.children && v.children.length > 0) {
      resObject.push({
        id: thisId,
        label: v.label,
        disabled: true,
        children: GetTreeDataDisabled(v.children, level + 1)
      });
    } else {
      resObject.push({
        id: thisId,
        label: v.label,
        disabled: true,
        children: GetTreeDataDisabled(v.children, level + 1)
      });
    }
  });
  return resObject;
}

// 增加按钮和全选disabled
function GetMenusTreeDataDisabled(data, level) {
  var resObject = [];
  if (!data || data.length < 1) {
    return null;
  }
  data.forEach((v, i) => {
    if (v.children && v.children.length > 0) {
      resObject.push({
        id: v.id,
        label: v.menuName,
        isMenu: v.isMenu,
        children: GetMenusTreeDataDisabled(v.children, level + 1),
        buttonIds: [],
        checkAll: ""
      });
    } else {
      resObject.push({
        id: v.id,
        label: v.menuName,
        isMenu: v.isMenu,
        children: GetMenusTreeDataDisabled(v.children, level + 1),
        buttonIds: [],
        checkAll: "",
        menuId: v.id
      });
    }
  });
  return resObject;
}
/**
 * 数据字典Table表格转换勿动
 * GettableTreeDataDisabled
 */
function GettableTreeDataDisabled(data, level) {
  var resObject = [];
  if (!data || data.length < 1) {
    return null;
  }
  data.forEach((v, i) => {
    if (v.children && v.children.length > 0) {
      resObject.push({
        id: v.id,
        label: v.menuName,
        isMenu: v.isMenu,
        children: GettableTreeDataDisabled(v.children, level + 1),
        checkAll: "",
        menuIcon: v.menuIcon,
        checkAll: "",
        menuId: v.id,
        parentID: v.parentID,
        badgeNum: 0
      });
    } else {
      resObject.push({
        id: v.id,
        label: v.menuName,
        isMenu: v.isMenu,
        children: GettableTreeDataDisabled(v.children, level + 1),
        checkAll: "",
        menuId: v.id,
        menuIcon: v.menuIcon
      });
    }
  });
  return resObject;
}
/**
 * GettableTreeDataDisabled
 * 右边数据字典
 *
 */
function GetRighttableTreeDataDisabled(data, level) {
  var resObject = [];
  if (!data || data.length < 1) {
    return null;
  }
  data.forEach((v, i) => {
    if (v.children && v.children.length > 0) {
      resObject.push({
        id: v.id,
        label: v.name,
        types: v.type,
        children: GetRighttableTreeDataDisabled(v.children, level + 1),
        munid: v.menuID,
        parteId:v.parentID
      });
    } else {
      resObject.push({
        id: v.id,
        label: v.name,
        types: v.type,
        children: GetRighttableTreeDataDisabled(v.children, level + 1),
        munid: v.menuID,
        parteId:v.parentID
      });
    }
  });
  return resObject;
}

var AppTreeUtil = {
  ToSelectOptions: ToSelectOptions,
  ToTreeData: ToTreeData,
  ToTreeDataDisabled: GetTreeDataDisabled,
  ToMenusTreeDataDisabled: GetMenusTreeDataDisabled,
  ToTableTreeDataDisabled: GettableTreeDataDisabled,
  ToTableRightTreeDataDisabled: GetRighttableTreeDataDisabled
};

export default AppTreeUtil;
